package com.yueyang.sojourn.server.controller.merch;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.dto.AddMerchDto;
import com.yueyang.sojourn.server.model.dto.EditMerchDto;
import com.yueyang.sojourn.server.model.vo.SojournMerchVo;
import com.yueyang.sojourn.server.service.sojourn.merch.ReadMerchService;
import com.yueyang.sojourn.server.service.sojourn.merch.WriteMerchService;
import com.yueyang.uc.model.UcUserV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author yemingxing
 * @summary
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-17
 */
@RestController
@RequestMapping("/sojourn/merch")
@Api(tags = "商品 API")
public class SojournMerchController extends BaseController {

    @Autowired
    private WriteMerchService writeMerchService;
    @Autowired
    private ReadMerchService readMerchService;

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "新建商品", notes = "返回新建的商品id")
    @PostMapping("/add")
    public Long addMerch(@RequestBody @Validated AddMerchDto merchDto, BindingResult bindingResult) {

        FieldError fieldError = bindingResult.getFieldError();
        if (Objects.nonNull(fieldError)) {
            JAssert.justFailed(fieldError.getDefaultMessage());
        }

        UcUserV1 UcUserV1 = super.getLoginUser();
        long loginUserId = UcUserV1.getId();
        return writeMerchService.addMerch(loginUserId, merchDto);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "编辑商品")
    @PostMapping("/edit")
    public void editMerch(@RequestBody @Validated EditMerchDto merchDto, BindingResult bindingResult) {

        FieldError fieldError = bindingResult.getFieldError();
        if (Objects.nonNull(fieldError)) {
            JAssert.justFailed(fieldError.getDefaultMessage());
        }

        UcUserV1 UcUserV1 = super.getLoginUser();
        long loginUserId = UcUserV1.getId();
        writeMerchService.editMerch(loginUserId, merchDto);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "商品上架/下架")
    @PostMapping("/alterStatus")
    public void editShelfStatus(@ApiParam(value = "商品id", required = true) @RequestParam Long merchId,
                                @ApiParam(value = "上架状态(枚举值)", required = true) @RequestParam Integer shelfStatus) {

        UcUserV1 UcUserV1 = super.getLoginUser();
        long loginUserId = UcUserV1.getId();
        writeMerchService.editShelfStatus(loginUserId, merchId, shelfStatus);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "删除商品")
    @PostMapping("/delete/{merchId}")
    public void deleteMerch(@ApiParam(value = "商品id", required = true) @PathVariable Long merchId) {

        UcUserV1 UcUserV1 = super.getLoginUser();
        long loginUserId = UcUserV1.getId();
        writeMerchService.deleteMerch(loginUserId, merchId);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "判断商品是否过期", notes = "true代表过期，反之则没过期")
    @GetMapping("/available/{merchId}")
    public boolean isMerchAvailable(@ApiParam(value = "商品id", required = true) @PathVariable Long merchId) {

        UcUserV1 UcUserV1 = super.getLoginUser();
        long loginUserId = UcUserV1.getId();
        return readMerchService.isMerchAvailable(loginUserId, merchId);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "商品列表分页查询")
    @GetMapping("/page")
    public PageInfo<SojournMerchVo> findPage(@ApiParam(value = "关键词") @RequestParam(required = false) String keyWord,
                                             @ApiParam(value = "所属项目id") @RequestParam(required = false) Long projectId,
                                             @ApiParam(value = "上架状态(枚举值)") @RequestParam(required = false) Integer shelfStatus,
                                             @RequestParam int pageNo,
                                             @RequestParam int pageSize) {
        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        return readMerchService.findPage(loginUserId, keyWord, projectId, shelfStatus, pageNo, pageSize);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "根据商品id查询商品详情")
    @GetMapping("/{merchId}")
    public SojournMerchVo findById(@ApiParam(value = "商品id") @PathVariable Long merchId) {
        return readMerchService.findVoById(merchId);
    }
}
